package com.xigua.xcar.widgets.ShopInfo;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xigua.xcar.R;
import com.xigua.xcar.data.ShopInfoData;

public class ShopInfoAdapter extends BaseAdapter {
	private ArrayList<ShopInfoData> mArray = null;
	private LayoutInflater mInflater = null;

	public ShopInfoAdapter(Context context) {
		if (null != context) {
			mInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
	}

	public void setData(ArrayList<ShopInfoData> array) {
		mArray = array;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mArray.size();
	}

	@Override
	public Object getItem(int arg0) {
		return mArray.get(arg0 % mArray.size());
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	void updateView(View convertView, ShopInfoData data, int index) {
		ImageView image = null;
		TextView content = null;
		image = (ImageView) convertView
				.findViewById(R.id.image_shopdetail_icon);
		content = (TextView) convertView
				.findViewById(R.id.txt_shopdetail_content);
		if (0 != data.getmResId()) {
			image.setBackgroundResource(data.getmResId());
		} else {
			image.setVisibility(View.GONE);
		}
		if (null != data.getmContent()) {
			content.setText(data.getmContent());
		} else {
			content.setVisibility(View.GONE);
		}
		convertView.setTag(data);
		if(0 == index){
			convertView.setBackgroundResource(R.drawable.list_back_top);
		}else if((mArray.size()-1) == index){
			convertView.setBackgroundResource(R.drawable.list_back_bottom);
		}else{
			convertView.setBackgroundResource(R.drawable.list_back_middle);
		}
		if(mArray.size()==1){
			convertView.setBackgroundResource(R.drawable.about_back);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		position = position % (mArray.size());
		ShopInfoData data = mArray.get(position);
		if (null == convertView) {
			convertView = mInflater.inflate(R.layout.shopdetail_shopinfoitem,
					null);
		}
		updateView(convertView, data, position);
		return convertView;
	}

}
